package com.sw.small_world.fragment.home.search;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.MainActivity;
import com.sw.small_world.fragment.base.BaseFragment;
import com.sw.small_world.utils.CommonUtils;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.ShareUtils;
import com.sw.small_world.utils.StartUtils;
import com.sw.small_world.utils.ToastUtil;
import com.sw.small_world.widgets.MarkStreamLayout;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 */

public class HomeSearchFragment extends BaseFragment {


    @Bind(R.id.tv_back)
    TextView tv_back;
    @Bind(R.id.search_history)
    MarkStreamLayout searchHistory;
    @Bind(R.id.search_discover)
    MarkStreamLayout searchDiscover;
    @Bind(R.id.delete_search_history)
    LinearLayout deleteSearchHistory;
    private MainActivity activity;
    ArrayList<String> historyList = new ArrayList<>();
    ArrayList<String> discoverList = new ArrayList<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) getActivity();
        activity.rlActionbar.setVisibility(View.GONE);
    }

    @Override
    protected Object requestData() {
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View getSuccessView() {
        View view = View.inflate(activity, R.layout.fragment_home_search, null);
        ButterKnife.bind(this, view);
        tv_back.setOnClickListener(this);
        initSearchHistory();
        initSearchDiscover();
        return view;
    }

    private void initSearchHistory() {
        historyList.add("男朋友");
        historyList.add("车");
        historyList.add("健康");
        historyList.add("股票行情");
        historyList.add("男朋友");
        historyList.add("车");
        historyList.add("天气");
        historyList.add("股票行情");
        addMarkStream(historyList, searchHistory);
    }

    private void initSearchDiscover() {
        discoverList.add("男朋友");
        discoverList.add("车");
        discoverList.add("健康");
        discoverList.add("股票行情");
        discoverList.add("男朋友");
        discoverList.add("车");
        discoverList.add("天气");
        discoverList.add("股票行情");
        addMarkStream(discoverList, searchDiscover);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                activity.rlActionbar.setVisibility(View.VISIBLE);
                activity.onBackPressed();
                activity.homeSearch.setVisibility(View.VISIBLE);
                ShareUtils.putInt(ShareUtils.IS_HOME,0);
                break;
        }
    }

    /**
     * 添加流标签
     */
    private void addMarkStream(final ArrayList<String> list, MarkStreamLayout layout) {
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = CommonUtils.sp2px(5);
        lp.rightMargin = CommonUtils.sp2px(5);
        lp.topMargin = CommonUtils.sp2px(5);
        lp.bottomMargin = CommonUtils.sp2px(5);

        for (int i = 0; i < list.size(); i++) {
            TextView view = new TextView(getActivity());
            view.setText(list.get(i));
            view.setTextSize(13);
            view.setTextColor(Color.parseColor("#999999"));
            view.setBackgroundResource(R.drawable.shape_round_grey);
            view.setPadding(CommonUtils.sp2px(10), CommonUtils.sp2px(3),
                    CommonUtils.sp2px(10), CommonUtils.sp2px(3));
            final int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StartUtils.startActivityById(activity,ConstantUtils.SEARCH_RESULT);
                }
            });
            layout.addView(view, lp);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);

    }
}
